package org.dydl.controller.system;

import java.util.List;

import org.dydl.common.dto.ResponseMessage;
import org.dydl.model.system.Delivery;
import org.dydl.model.system.DeliveryDetail;
import org.dydl.model.system.vo.DeliveryVO;
import org.dydl.service.system.ITranslateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 运输管理接口
 * 
 * @author 张华城
 *
 */

@RestController
@RequestMapping("/api/translate")
public class TranslateController {
	
	@Autowired
	private ITranslateService translateService;

	/**
	 * 查询运输管理列表页、模糊查询
	 * @param page
	 * @param rows
	 * @param deliveryNoteNo
	 * @return
	 */
	@RequestMapping("/list")
	public ResponseMessage list(int page, int rows, String deliveryNoteNo){
		
		ResponseMessage res = translateService.list(page, rows, deliveryNoteNo);
		
		return res;
	}
	
	/**
	 * 新增运输信息
	 * @param delivery
	 * @return
	 */
	@RequestMapping("/save")
	public ResponseMessage save(@RequestBody DeliveryVO deliveryVO){
		
		
		return translateService.save(deliveryVO);
	}
	
	/**
	 * 删除运输信息
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public boolean delete(int[] ids){
		if (ids.length != 0) {
			for (int i = 0; i < ids.length; i++) {
				translateService.delete(ids[i]);
			}
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 查询送货单详情
	 * @param deliveryNoteNo
	 * @return
	 */
	@RequestMapping("/getdetail")
	public List<DeliveryDetail> getDetailList(@RequestParam(value = "deliveryNoteNo", required = false)String deliveryNoteNo){
		
		List<DeliveryDetail> res = translateService.getDetailList(deliveryNoteNo);
		
		return res;
	}
	
	/**
	 * 根据运单号查询实体
	 * @param deliveryNoteNo
	 * @return
	 */
	@RequestMapping("/findByDeliveryNoteNo")
	public Delivery findBydeliveryNoteNo(@RequestParam(value = "deliveryNoteNo", required = false)String deliveryNoteNo){
		
		Delivery delivery = translateService.findByDeliveryNoteNo(deliveryNoteNo);
		
		return delivery;
	}
	
	/**
	 * 生成运单号
	 * @return
	 */
	@RequestMapping("/getDeliveryNoteNo")
	public String getDeliveryNoteNo(){
		
		String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		
		char random = chars.charAt((int)(Math.random()*26));
		
		String deliveryNoteNo = random+""+ System.currentTimeMillis();
		
		return deliveryNoteNo;
	}
	
	/**
	 * 审核
	 * @param ids
	 * @param orderNo
	 * @param deliveryNoteNo
	 * @return
	 */
	@RequestMapping("/toExamine")
	public ResponseMessage toExamine(Integer[] ids,String orderNo,String deliveryNoteNo) {
		ResponseMessage res = null;
		if (ids.length != 0) {
			for (int i = 0; i < ids.length; i++) {
				res = translateService.changeExamine(ids[i],orderNo,deliveryNoteNo);
			}
		} 
		return res;
	}
	
	@RequestMapping("/sign")
	public ResponseMessage sign(String deliveryNoteNo,String recipient,String signTime){
		
		return translateService.doSign(deliveryNoteNo,recipient,signTime);
		
	}
	
}
